package com.stylefeng.guns.rest.modular.cinema;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.CinemaServiceApi;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.rest.common.persistence.mapper.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CinemaServiceApiImpl
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/9/15  9:36
 * @Version 1.0
 */
@Component(value = "cinemaServiceApi")
@Slf4j
@Service(interfaceClass = CinemaServiceApi.class, executes = 10)
public class CinemaServiceApiImpl implements CinemaServiceApi {

    @Autowired
    private MoocAreaDictTMapper moocAreaDictMapper;

    @Autowired
    private MoocBrandDictTMapper moocBrandDictTMapper;

    @Autowired
    private MoocCinemaTMapper moocCinemaTMapper;

    @Autowired
    private MoocFieldTMapper moocFieldTMapper;

    @Autowired
    private MoocHallDictTMapper moocHallDictTMapper;

    @Autowired
    private MoocHallFilmInfoTMapper moocHallFilmInfoTMapper;

    //1、根据CinemaQueryVO，查询影院列表
    @Override
    public Page<CinemaVo> getCinemas(CinemaQueryModel queryModel) {

        //返回数据
        List<CinemaVo> cinemaVos = new ArrayList<>();
        cinemaVos.clear();

        Page<MoocCinemaT> page = new Page<>(queryModel.getNowPage(), queryModel.getPageSize());

        //判断传入的 查询 参数是否 是99
        EntityWrapper<MoocCinemaT> entityWrapper = new EntityWrapper<>();
        if (queryModel.getBrandId() != 99) {
            entityWrapper.eq("brand_id", queryModel.getBrandId());
        }

        if (queryModel.getDistrictId() != 99) {
            entityWrapper.eq("area_id", queryModel.getDistrictId());
        }

        if (queryModel.getHallType() != 99) {
            entityWrapper.like("hall_ids", "%#" + queryModel.getHallType() + "#%");
        }

        //业务实体
        List<MoocCinemaT> moocCinemaTS = moocCinemaTMapper.selectPage(page, entityWrapper);
        for (MoocCinemaT cinemaT : moocCinemaTS) {
            CinemaVo cinemaVo = new CinemaVo();
            cinemaVo.setUuid(cinemaT.getUuid() + "")
                    .setCinemaName(cinemaT.getCinemaName())
                    .setAddress(cinemaT.getCinemaAddress())
                    .setMinimumPrice(cinemaT.getMinimumPrice() + "");

            cinemaVos.add(cinemaVo);
        }

        long counts = moocCinemaTMapper.selectCount(entityWrapper);

        Page<CinemaVo> cinemaVoPage = new Page<>();
        cinemaVoPage.setRecords(cinemaVos);
        cinemaVoPage.setSize(queryModel.getPageSize());
        cinemaVoPage.setTotal(counts);
        return cinemaVoPage;
    }

    //2、根据条件获取品牌列表 除了99 其他数字isActive
    @Override
    public List<BrandVo> getBrands(int brandId) {
        boolean flag = false;

        MoocBrandDictT moocBrandDictT = moocBrandDictTMapper.selectById(brandId);
        if (brandId == 99 || moocBrandDictT == null || moocBrandDictT.getUuid() == null) {
            flag = true;
        }

        //查询所有列表
        List<MoocBrandDictT> moocBrandDictTS = moocBrandDictTMapper.selectList(null);

        List<BrandVo> brandVos = new ArrayList<>();
        brandVos.clear();
        //判断flag  如果为true 则将99置为isActive
        for (MoocBrandDictT brandDictT : moocBrandDictTS) {
            BrandVo brandVo = new BrandVo();
            brandVo.setBeandId(brandDictT.getUuid() + "")
                    .setBrandName(brandDictT.getShowName());
            if (flag) {
                if (brandDictT.getUuid() == 99) {
                    brandVo.setActive(true);
                }
            } else {
                if (brandDictT.getUuid() == brandId) {
                    brandVo.setActive(true);
                }
            }
            brandVos.add(brandVo);
        }

        return brandVos;
    }

    //3、获取行政区域列表
    @Override
    public List<AreaVo> getAreas(int areaId) {
        boolean flag = false;

        MoocAreaDictT areaDictT1 = moocAreaDictMapper.selectById(areaId);
        if (areaId == 99 || areaDictT1 == null || areaDictT1.getUuid() == null) {
            flag = true;
        }

        //查询所有列表
        List<MoocAreaDictT> moocAreaDictTS = moocAreaDictMapper.selectList(null);

        List<AreaVo> areaVos = new ArrayList<>();
        areaVos.clear();
        //判断flag  如果为true 则将99置为isActive
        for (MoocAreaDictT areaDictT : moocAreaDictTS) {
            AreaVo areaVo = new AreaVo();
            areaVo.setAreaId(areaDictT.getUuid() + "")
                    .setAreaName(areaDictT.getShowName());
            if (flag) {
                if (areaDictT.getUuid() == 99) {
                    areaVo.setActive(true);
                }
            } else {
                if (areaDictT.getUuid() == areaId) {
                    areaVo.setActive(true);
                }
            }
            areaVos.add(areaVo);
        }

        return areaVos;
    }

    //4、获取影厅类型列表
    @Override
    public List<HallTypeVo> getHallTypes(int hallType) {
        boolean flag = false;

        MoocHallDictT moocHallDictT = moocHallDictTMapper.selectById(hallType);
        if (hallType == 99 || moocHallDictT == null || moocHallDictT.getUuid() == null) {
            flag = true;
        }

        //查询所有列表
        List<MoocHallDictT> moocHallDictTS = moocHallDictTMapper.selectList(null);

        List<HallTypeVo> hallTypeVos = new ArrayList<>();
        hallTypeVos.clear();
        //判断flag  如果为true 则将99置为isActive
        for (MoocHallDictT hallDictT : moocHallDictTS) {
            HallTypeVo hallTypeVo = new HallTypeVo();
            hallTypeVo.setHallTypeId(hallDictT.getUuid() + "")
                    .setHallTypeName(hallDictT.getShowName());
            if (flag) {
                if (hallDictT.getUuid() == 99) {
                    hallTypeVo.setActive(true);
                }
            } else {
                if (hallDictT.getUuid() == hallType) {
                    hallTypeVo.setActive(true);
                }
            }
            hallTypeVos.add(hallTypeVo);
        }

        return hallTypeVos;
    }

    //5、根据影院编号，获取影院信息
    @Override
    public CinemaInfoVo getCinemaInfoById(int cinemaId) {
        //数据实体
        MoocCinemaT moocCinemaT = moocCinemaTMapper.selectById(cinemaId);
        //将数据实体转换为业务实体。
        CinemaInfoVo cinemaInfoVo = new CinemaInfoVo();
        cinemaInfoVo.setCinemaId(moocCinemaT.getUuid() + "")
                .setCinemaUrl(moocCinemaT.getImgAddress())
                .setCinemaPhone(moocCinemaT.getCinemaPhone())
                .setCinemaName(moocCinemaT.getCinemaName())
                .setCinemaAddress(moocCinemaT.getCinemaAddress());


        return cinemaInfoVo;
    }

    //6、获取所有电影的信息和对应的放映场次信息，根据影院编号
    @Override
    public List<FilmInfoVo> getFilmInfoByCinemaId(int cinemaId) {
        List<FilmInfoVo> filmInfos = moocFieldTMapper.getFilmInfos(cinemaId);
        log.info("获取所有电影的信息和对应的放映场次信息，根据影院编号 -- > " + "\n" + filmInfos);
        return filmInfos;
    }

    //7、根据放映场次ID获取放映信息
    @Override
    public HallInfoVo getFilmFieldInfo(int fieldId) {
        HallInfoVo hallInfoVo = moocFieldTMapper.getHallInfoVo(fieldId);
        log.info("根据放映场次ID获取放映信息 --> " + "\n" + hallInfoVo);
        return hallInfoVo;
    }

    //8、根据放映场次查询播放的电影编号，然后根据电影编号获取对应的电影信息
    @Override
    public FilmInfoVo getFilmInfoByFieldId(int fieldId) {

        FilmInfoVo filmInfoVo = moocFieldTMapper.getFilmInfoVo(fieldId);
        log.info("根据放映场次查询播放的电影编号 --> " + "\n" + filmInfoVo);

        return filmInfoVo;
    }


    @Override
    public OrderQueryVo getOrderNeed(int fieldId) {

        OrderQueryVo orderQueryVo = new OrderQueryVo();

        MoocFieldT moocFieldT = moocFieldTMapper.selectById(fieldId);
        if (moocFieldT != null) {
            orderQueryVo.setCinemaId(moocFieldT.getCinemaId() + "").setFilmPrice(moocFieldT.getPrice() + "");
        }
        return orderQueryVo;
    }
}
